<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="成品名称" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入成品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="渠道" prop="channelId">
        <el-input
          v-model="queryParams.channelId"
          placeholder="请输入渠道"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="价格" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入价格"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="订购时间" prop="orderTime">
<!--        <el-input
          v-model="queryParams.orderTime"
          placeholder="请输入订购时间"
          clearable
          @keyup.enter="handleQuery"
        />-->
        <el-date-picker clearable
            v-model="orderTime"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="订购信息" prop="resultMsg">
        <el-input
          v-model="queryParams.resultMsg"
          placeholder="请输入订购信息"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="兑换关闭时间" prop="closeTime" label-width="99px">
        <el-date-picker clearable
          v-model="closeTime"
                        value-format="YYYY-MM-DD"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期" >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['merchandise:orderInfo:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['merchandise:orderInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchandise:orderInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['merchandise:orderInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
      <el-table-column label="手机号" align="center" prop="phone" width="110"/>
      <el-table-column label="订购时间" align="center" prop="orderTime" width="160" >
        <template #default="scope">
<!--          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>-->
          <span>{{scope.row.orderTime.substring(0,4)}}-{{scope.row.orderTime.substring(4,6)}}-{{scope.row.orderTime.substring(6,8)}} {{scope.row.orderTime.substring(8,10)}}:{{scope.row.orderTime.substring(10,12)}}:{{scope.row.orderTime.substring(12,14)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="productId" width="200">
        <template #default="scope">
          <span>{{scope.row.productId.substring(3)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="渠道" align="center" prop="channelId"  width="100"/>
<!--      <el-table-column label="价格" align="center" prop="amount" />-->
      <el-table-column label="订购code" align="center" prop="resultCode" />
      <el-table-column label="订购信息" align="center" prop="resultMsg"  width="100"/>
      <el-table-column label="是否允许兑换" align="center" prop="closeStatus" >
        <template #default="scope">
          <span v-if="scope.row.closeStatus === '0' || scope.row.resultCode !== '00000'">不允许</span>
          <span v-if="scope.row.closeStatus !== '0' && scope.row.resultCode === '00000' ">允许</span>
        </template>
      </el-table-column>
      <el-table-column label="兑换关闭时间" align="center" prop="closeTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.closeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
        <template #default="scope">
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['merchandise:orderInfo:edit']">修改</el-button>-->
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['merchandise:orderInfo:remove']">删除</el-button>
          <el-button link type="primary" icon="Edit" @click="handle(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订购信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderInfoRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="成品名称" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入成品名称" readonly />
        </el-form-item>
        <el-form-item label="渠道" prop="channelId">
          <el-input v-model="form.channelId" placeholder="请输入渠道" readonly/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" readonly/>
        </el-form-item>
        <el-form-item label="价格" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入价格" readonly/>
        </el-form-item>
        <el-form-item label="订购时间" prop="orderTime">
          <el-input v-model="form.orderTime" placeholder="请输入订购时间" readonly />
        </el-form-item>
        <el-form-item label="订购信息" prop="resultMsg">
          <el-input v-model="form.resultMsg" placeholder="请输入订购信息" readonly />
        </el-form-item>
        <el-form-item label="是否允许兑换" prop="closeStatus">
          <el-select v-model="form.closeStatus" placeholder="是否允许兑换" style="width: 400px">
            <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="兑换关闭时间" prop="closeTime">
          <el-date-picker clearable
            v-model="form.closeTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择兑换时间">
          </el-date-picker>
        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrderInfo">
import { listOrderInfo, getOrderInfo, delOrderInfo, addOrderInfo, updateOrderInfo } from "@/api/merchandise/orderInfo";

const { proxy } = getCurrentInstance();

const orderInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const closeTime = ref([]);
const orderTime= ref([]);
const router = useRouter();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    productId: null,
    channelId: null,
    phone: null,
    amount: null,
    orderTime: null,
    resultCode: null,
    resultMsg: null,
    closeStatus: null,
    closeTime: null
  },
  statusOptions: [{
    value: '0',
    label: '不允许'
  }, {
    value: '1',
    label: '允许'
  }],
  rules: {
  }
});

const { queryParams, form, rules ,statusOptions} = toRefs(data);

/** 查询订购信息列表 */
function getList() {
  loading.value = true;
  let range = proxy.addDateRange(queryParams.value,closeTime.value,'CloseTime');
  range = proxy.addDateRange(queryParams.value,orderTime.value, 'OrderTime');
  // listOrderInfo(queryParams.value).then(response => {
 listOrderInfo(range).then(response => {
    orderInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    linkId: null,
    productId: null,
    channelId: null,
    outTradeNo: null,
    phone: null,
    amount: null,
    orderTime: null,
    orderStatus: null,
    resultCode: null,
    resultMsg: null,
    errorCode: null,
    closeStatus: null,
    closeTime: null
  };
  proxy.resetForm("orderInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  closeTime.value=null;
  orderTime.value = null;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订购信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getOrderInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订购信息";
  });
}

function handle(row) {
  router.push({ path: "/orderInfo/user", query: {phone:row.phone} }).then(() => { window.location.reload(); });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOrderInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrderInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除订购信息编号为"' + _ids + '"的数据项？').then(function() {
    return delOrderInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('merchandise/orderInfo/export', {
    ...queryParams.value
  }, `orderInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
